<?php

namespace app\api\controller;

use app\services\TextService;
use support\Request;

class TextController
{
    public function getArticles(Request $request)
    {
        try {
            $categoryId = (int) $request->post('category_id', 0);
            $page = (int) $request->post('page', 1);
            $pageSize = (int) $request->post('page_size', 10);
            $keyword = $request->post('keyword', null);

            $articles = TextService::getArticlesByCategoryId($categoryId, $page, $pageSize, $keyword);

            return ok_rsp('成功', $articles);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }

    }

    public function getTop10ArticlesByCategoryId(Request $request)
    {
        try {
            $categoryId = (int) $request->post('category_id', 0);

            $articles = TextService::getTop10ArticlesByCategoryId($categoryId);

            return ok_rsp('成功', $articles);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }

    }

    public function getArticleDetail(Request $request)
    {
        try {
            $articleId = (int) $request->post('id', 0);
            $article = TextService::getArticleDetail($articleId);

            return ok_rsp('成功', $article);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }

    }
}